#!/bin/bash
#
# cliradio - listen to internet radio from the comfort of the terminal
#
# This program will enable you to stream internet radios and tune into other  
# channels in the command line via the mpg123 utility. You can use pretty much 
# any station playlist (.pls format) online, including those at SomaFM.
#
# Tor works very nicely with it too. I recommend installing `torsocks` and then
# just running `torsocks cliradio`.
#
# To change stations, press Ctrl-C twice to return to the selection menu.
#
#    Copyright (C) 2016 - Klaus Zimmermann <https://quitter.se/kzimmermann>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

if ! test -f $HOME/.radiolist;
then
    echo "Radio list not found in home directory."
    echo "Creating radio list ~/.radiolist for you instead."
    echo "Make sure to add your own radios like the example ones"
cat > $HOME/.radiolist <<EOF
# Example of how to include radios and their addresses on your own config
# 
# You can use anything that mpg123 can use, .pls works great. SomaFM stations
# are 100% compatible.
#
# To include a radio, pair a title and an address like this
#
#   name>Name of radio station
#   url>http://example.com/etc/playlist.pls?bitrate=128
#
# Comments can be included as well, as lines starting with '#'
#

name>Dubstep Beyond - Soma FM
url>http://somafm.com/dubstep.pls

# Very nice station
name>Underground 80s - Soma FM
url>http://somafm.com/u80s.pls

name>The Trip - Soma FM
url>http://somafm.com/thetrip.pls

name>Hip Hop
url>http://yp.shoutcast.com/sbin/tunein-station.pls?id=288144

name>DNBRADIO
url>http://yp.shoutcast.com/sbin/tunein-station.pls?id=23036

name>Ibiza Party Radio
url>http://yp.shoutcast.com/sbin/tunein-station.pls?id=99180204
EOF
fi

if test -z $(which mpg123)
then
    echo "mpg123 is not installed."
    echo "Please install mpg123 to use this program."
    exit 1
fi

# Variable declaration for this script:
desc_index=1
url_index=1

declare -a descriptions
declare -a urls

# Push the configurations over
while read line
do
    if [[ -n "$line" ]];
    then
        case ${line:0:1} in
            "#" )
                # skip comments 
            ;;
            "n" )
                descriptions[$desc_index]="$(
                    echo "$line" | cut -d ">" -f 2
                )"
                desc_index=$(expr $desc_index + 1)
            ;;
            "u" )
                urls[$url_index]="$(
                    echo "$line" | cut -d ">" -f 2
                )"
                url_index=$(expr $url_index + 1)
            ;;
            * )
                # Don't eff the config up!
                echo "Error - malformed configuration file at this line:"
                echo "$line"
                exit 1
            ;;
        esac
    fi
done < $HOME/.radiolist


# Main loop :)
while true
do
    echo "We have the following radios available in your config"

    i=1

    for radio in "${descriptions[@]}"
    do
        echo "$i - ${radio}"
        i=`expr $i + 1`
    done
    printf "Which one do you want to stream? ('q' to quit) "
    read choice

    if [[ "$choice" == "q" ]]; then
        echo "Exiting..."
        exit 0
    elif [[ "$choice" -ge "$i" || "$choice" -le 0 ]]; then
        echo "Choice out of range! Try again."
    else
        echo "========"
        echo "Now listening to ${descriptions[$choice]}"
        echo "Press Ctrl+C to change radios"
        processed=${urls[$choice]}
        # thanks, mpg123!
        mpg123 -@ "$processed" ||
            echo "Error: URL points to a nonexistent radio!"
        echo 
        echo "========"
        echo "You were just listening to ${descriptions[$choice]}"
    fi
done
